import flask

app = flask.Flask(__name__)     

@app.route("/index",methods=['get','post'])                 
def index():
    currency = flask.request.values.get("currency")
    file = open(file="rates.csv",mode="r",encoding="utf-8")
    rows = file.readlines()     #迭代的按行读取，并将读取到的数据放入的list，一个list元素中存放一行数据。
    result="<table border='1'>"
    if currency == None or currency == "":        #输出全部外汇信息
        for row in rows:
            result+="<tr>"
            items = row.split(",")
            for item in items:
                result+="<td>"+item+"</td>"
            result+="</tr>"
    else:                       #根据用户输入的数据输出具体某一个外汇信息
        i=0
        for row in rows:
            i=i+1
            s=row.split(",")
            if s[0]==currency or i==1:    #s[0]指的是外汇名称，同时加入i==1是为了显示表头
                result=result+"<tr>"
                for t in s:
                    result+="<td>"+t+"</td>"
                result=result+"</tr>"
    result+="</table>"
    return result

app.debug = True
app.run()
